<template>
  <el-row style="width:100%">
    <el-col :span="20">{{ treeNode.name }}</el-col>
    <el-col :span="4">
      <el-row>
        <el-col :span="12">{{ treeNode.manager }}</el-col>
        <el-col :span="12">
          <el-dropdown @command="handleCommand">
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item command="edit" v-if="!isRoot"
                >查看部门</el-dropdown-item
              >
              <el-dropdown-item command="del" v-if="!isRoot"
                >删除部门</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartments } from '@/api/departments.js'
export default {
  props: {
    treeNode: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    handleCommand (cmd) {
      switch (cmd) {
        case 'add':
          // 新增
          this.$emit('addDept', this.treeNode.id);
          break
        case 'edit':
          // 修改
          this.$emit('editDept',this.treeNode.id)
          break
        case 'del':
          // 删除
          this.$confirm('请问确定要删除吗?', '温馨提示:', {
            confirmButtonText: '确定',
            cancelButtonText: '取消'
          }).then(async () => {
            await delDepartments(this.treeNode.id)
            this.$message.success(this.treeNode.name + '删除成功')
            // 再发一个请求获取最新列表
            this.$emit('delDept')
          })
          break;
      }
    }
  }
}
</script>

<style></style>
